package com.jeepig.web.controller.evaluation;

import com.jeepig.common.annotation.Log;
import com.jeepig.common.core.controller.BaseController;
import com.jeepig.common.core.domain.AjaxResult;
import com.jeepig.common.core.page.TableDataInfo;
import com.jeepig.common.enums.BusinessType;
import com.jeepig.common.utils.poi.ExcelUtil;
import com.jeepig.evaluation.domain.SelfEvaluation;
import com.jeepig.evaluation.service.ISelfEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 自我评价结果Controller
 *
 * @author guoxb
 * @date 2025-11-01
 */
@RestController
@RequestMapping("/evaluation/selfEvaluation")
public class SelfEvaluationController extends BaseController
{
    @Autowired
    private ISelfEvaluationService selfEvaluationService;

    /**
     * 查询自我评价结果列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(SelfEvaluation selfEvaluation)
    {
        startPage();
        List<SelfEvaluation> list = selfEvaluationService.selectSelfEvaluationList(selfEvaluation);
        return getDataTable(list);
    }

    /**
     * 根据人员查询自评价结果
     *
     * @param memberId
     * @return
     */
    @GetMapping("/list/{memberId}")
    public AjaxResult list(@PathVariable("memberId") Long memberId){
        List<SelfEvaluation> selfEvaluations = selfEvaluationService.selectSelfEvaluationByMemberId(memberId);
        return AjaxResult.success(selfEvaluations);
    }
    /**
     * 导出自我评价结果列表
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:export')")
    @Log(title = "自我评价结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SelfEvaluation selfEvaluation)
    {
        List<SelfEvaluation> list = selfEvaluationService.selectSelfEvaluationList(selfEvaluation);
        ExcelUtil<SelfEvaluation> util = new ExcelUtil<SelfEvaluation>(SelfEvaluation.class);
        util.exportExcel(response, list, "自我评价结果数据");
    }

    /**
     * 获取自我评价结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(selfEvaluationService.selectSelfEvaluationById(id));
    }

    /**
     * 保存自我评价结果
     * @param selfEvaluations
     * @return
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:add')")
    @Log(title = "保存自我评价结果", businessType = BusinessType.INSERT)
    @PostMapping("/save/{memberId}")
    public AjaxResult save(@RequestBody List<SelfEvaluation> selfEvaluations, @PathVariable("memberId") Long memberId){
        //根据memberId删除现有的评价
        selfEvaluationService.deleteSelfEvaluationByMemberId(memberId);
        for(SelfEvaluation selfEvaluation:selfEvaluations){
            selfEvaluation.setMemberId(memberId);
            selfEvaluationService.insertSelfEvaluation(selfEvaluation);
        }
        return AjaxResult.success("评价成功！");
    }

    /**
     * 新增自我评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:add')")
    @Log(title = "自我评价结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SelfEvaluation selfEvaluation)
    {
        return toAjax(selfEvaluationService.insertSelfEvaluation(selfEvaluation));
    }

    /**
     * 修改自我评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:edit')")
    @Log(title = "自我评价结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SelfEvaluation selfEvaluation)
    {
        return toAjax(selfEvaluationService.updateSelfEvaluation(selfEvaluation));
    }

    /**
     * 删除自我评价结果
     */
    @PreAuthorize("@ss.hasPermi('evaluation:evaluation:remove')")
    @Log(title = "自我评价结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(selfEvaluationService.deleteSelfEvaluationByIds(ids));
    }
}
